﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace projekt_planet_express_doerr
{
    public class Arrow : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Model arrowMesh, meteorMesh;
        Texture2D meteoritTexture;
        float arrowRotStart;
        float metAX, metAY, metAZ;
        float metBX, metBY, metBZ;

        public enum LookAtState { metA, metB, none };
        public LookAtState lookAt = LookAtState.none;

        Matrix rot;
        Quaternion arrowRot;


        public Arrow(Game1 game)
            : base(game)
        {
            arrowRotStart = 0.0f;
        }

        protected override void LoadContent()
        {
            arrowMesh = Game.Content.Load<Model>("Arrow");
            meteorMesh = Game.Content.Load<Model>("sphere");
            meteoritTexture = Game.Content.Load<Texture2D>("meteoritTextur");
            base.LoadContent();
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            float turningSpeed = 0.25f;
            KeyboardState kb = Keyboard.GetState();

            switch (lookAt)
            {
                case LookAtState.metA:
                    if(kb.IsKeyDown(Keys.W))
                        metAY += turningSpeed;

                    if (kb.IsKeyDown(Keys.S))
                        metAY -= turningSpeed;

                    if (kb.IsKeyDown(Keys.A))
                        metAX += turningSpeed;

                    if (kb.IsKeyDown(Keys.D))
                        metAX -= turningSpeed;

                    if (kb.IsKeyDown(Keys.Q))
                        metAZ += turningSpeed;

                    if (kb.IsKeyDown(Keys.E))
                        metAZ -= turningSpeed;
                    break;
                case LookAtState.metB:
                    if(kb.IsKeyDown(Keys.W))
                        metBY += turningSpeed;

                    if (kb.IsKeyDown(Keys.S))
                        metBY -= turningSpeed;

                    if (kb.IsKeyDown(Keys.A))
                        metBX += turningSpeed;

                    if (kb.IsKeyDown(Keys.D))
                        metBX -= turningSpeed;

                    if (kb.IsKeyDown(Keys.Q))
                        metBZ += turningSpeed;

                    if (kb.IsKeyDown(Keys.E))
                        metBZ -= turningSpeed;
                    break;
                case LookAtState.none:
                    break;
                default:
                    break;
            }
            base.Update(gameTime);
        }

        internal void drawArrow(Matrix world, Matrix view, Matrix projection)
        {
            switch (lookAt)
            {
                case LookAtState.metA:
                    rot = Matrix.CreateLookAt(new Vector3(0,0,0), new Vector3(10 + metAX,0 - metAY, 20 + metAZ), Vector3.Down); //Down Spiegelung der Bewegung
                    arrowRot = Quaternion.CreateFromRotationMatrix(rot); //Vorher ging es mit CreateLookAt nicht weil kein Quaternion davon erstellt wurde
                    arrowRotStart = -90.0f; //damit Pfeil wenn er etwas anvisiert auch mit der Pfeilspitze drauf zeigt ^^
                    break;
                case LookAtState.metB:
                    rot = Matrix.CreateLookAt(new Vector3(0,0,0), new Vector3(-10 + metBX,0 - metBY, 20 + metBZ), Vector3.Down);
                    arrowRot = Quaternion.CreateFromRotationMatrix(rot);
                    arrowRotStart = -90.0f;
                    break;
                case LookAtState.none:
                    arrowRotStart = 90.0f;//damit Pfeil wenn er nichts anvisiert nach geradeaus zeigt
                    break;
                default:
                    break;
            }

            foreach (ModelMesh mesh in arrowMesh.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = world * Matrix.CreateScale(new Vector3(0.3f, 1.0f, 0.2f))
                                         * Matrix.CreateRotationX(MathHelper.ToRadians(arrowRotStart))
                                         * Matrix.CreateFromQuaternion(arrowRot)
                                         * Matrix.CreateTranslation(new Vector3(0, -10, 20));
                    effect.View = view;
                    effect.Projection = projection;
                }
                mesh.Draw();
            }
        }

        internal void drawMeteorA(Matrix world, Matrix view, Matrix projection)
        {
            foreach (ModelMesh mesh in meteorMesh.Meshes)
            {

                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = world * Matrix.CreateScale(new Vector3(5, 5, 5))
                                         * Matrix.CreateTranslation(new Vector3(10 + metAX, metAY, 20 + metAZ)); //Posi von MetA
                    effect.View = view;
                    effect.Projection = projection;
                    effect.TextureEnabled = true;
                    effect.Texture = meteoritTexture;
                }
                mesh.Draw();
            }
        }

        internal void drawMeteorB(Matrix world, Matrix view, Matrix projection)
        {
            foreach (ModelMesh mesh in meteorMesh.Meshes)
            {

                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = world * Matrix.CreateScale(new Vector3(5, 5, 5))
                                         * Matrix.CreateTranslation(new Vector3(-10 + metBX, metBY, 20 + metBZ)); //Posi von MetB
                    effect.View = view;
                    effect.Projection = projection;
                    effect.TextureEnabled = true;
                    effect.Texture = meteoritTexture;
                }
                mesh.Draw();
            }
        }
    }
}
